package com.ruoyi.cloudpc.controller;

import com.ruoyi.cloudpc.domain.CloudDevice;
import com.ruoyi.cloudpc.domain.CloudOrder;
import com.ruoyi.cloudpc.service.ICloudOrderService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单信息Controller
 *
 * @author Ten
 * @date 2025-09-13
 */
@RestController
@RequestMapping("/cloudpc/order")
public class CloudOrderController extends BaseController {
    @Autowired
    private ICloudOrderService cloudOrderService;

    /**
     * 查询订单信息列表
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(CloudOrder cloudOrder) {
        startPage();
        List<CloudOrder> list = cloudOrderService.selectCloudOrderList(cloudOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:order:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CloudOrder cloudOrder) {
        List<CloudOrder> list = cloudOrderService.selectCloudOrderList(cloudOrder);
        ExcelUtil<CloudOrder> util = new ExcelUtil<CloudOrder>(CloudOrder.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cloudOrderService.selectCloudOrderById(id));
    }

    /**
     * 新增订单信息
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:order:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CloudOrder cloudOrder) {
        return toAjax(cloudOrderService.insertCloudOrder(cloudOrder));
    }

//    /**
//     * 修改订单信息
//     */
////    @PreAuthorize("@ss.hasPermi('cloudpc:order:edit')")
//    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody CloudOrder cloudOrder) {
//        return toAjax(cloudOrderService.updateCloudOrder(cloudOrder));
//    }

    /**
     * 删除订单信息
     */
//    @PreAuthorize("@ss.hasPermi('cloudpc:order:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cloudOrderService.deleteCloudOrderByIds(ids));
    }

    /**
     * 支付订单
     */
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @PostMapping("/pay")
    public AjaxResult pay(@RequestParam Long id) {
        return toAjax(cloudOrderService.pay(id));
    }

    /**
     * 订单-开通
     */
    @Log(title = "订单开通", businessType = BusinessType.UPDATE)
    @PostMapping("/open")
    public AjaxResult open(@RequestBody CloudDevice device) {
        return toAjax(cloudOrderService.open(device));
    }

    /**
     * 我的云服务
     */
    @GetMapping("/myDeviceList")
    public TableDataInfo myDeviceList(CloudDevice cloudDevice) {
        startPage();
        List<CloudDevice> list = cloudOrderService.myDeviceList(cloudDevice);
        return getDataTable(list);
    }

    /**
     * 订单-修改设备名
     */
    @Log(title = "修改设备名", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDeviceId")
    public AjaxResult updateDeviceId(@RequestBody CloudDevice device) {
        return toAjax(cloudOrderService.updateDeviceId(device));
    }

}
